﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SeriesWatcher.DataModel
{
    class Season
    {
        private int number;
        public int Number
        {
            get { return number; }
            set { number = value; }
        }

        private List<Episode> episodes;

        public List<Episode> Episodes
        {
            get { return episodes; }
            set { episodes = value; }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public Season()
        {
            this.number = 1;
            this.episodes = new List<Episode>();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="number"></param>
        public Season(int number)
        {
            this.number = number;
            this.episodes = new List<Episode>();
        }

        /// <summary>
        /// Adding episode
        /// </summary>
        /// <returns>Pointer on episode</returns>
        public Episode AddEpisode()
        {
            Episode episode = new Episode();
            episodes.Add(episode);
            return episode;
        }

        /// <summary>
        /// Adding episode
        /// </summary>
        /// <param name="number">Number of episode</param>
        /// <returns>Pointer on episode</returns>
        public Episode AddEpisode(int number, string version)
        {
            if (number < 0)
                return null;

            /*foreach (Episode e in episodes)
            {
                if (e.Number == number)
                    return null;
            }*/

            Episode episode = new Episode(number, version);
            episodes.Add(episode);
            return episode;
        }

        public XmlElement SaveToXml(XmlDocument doc)
        {
            XmlElement element = doc.CreateElement("season");
            XmlAttribute att = doc.CreateAttribute("number");
            att.Value = Number.ToString();

            element.SetAttributeNode(att);

            foreach (Episode episode in Episodes)
            {
                element.AppendChild(episode.SaveToXml(doc));
            }

            return element;

        }
    }
}
